package edu.unika.aifb.graphindex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOError;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

import org.apache.log4j.Logger;

import edu.unika.aifb.graphindex.index.IndexDirectory;
import edu.unika.aifb.graphindex.index.IndexReader;
import edu.unika.aifb.graphindex.query.KeywordQuery;
import edu.unika.aifb.graphindex.searcher.keyword.evaluation.Row;
import edu.unika.aifb.graphindex.searcher.keyword.evaluation.TopKQueryEvaluator;

public class Evaluation {
	
	private static final Logger log = Logger.getLogger(Demo.class);
	
	public static void main(String[] args) throws Exception {
		OptionParser op = new OptionParser();
		op.accepts("id", "index directory").withRequiredArg().ofType(String.class).describedAs("index directory");
		op.accepts("rd", "result directory").withRequiredArg().ofType(String.class).describedAs("result directory");
		op.accepts("qd", "queries directory").withRequiredArg().ofType(String.class).describedAs("queries directory");
		op.accepts("nq", "number of generated queries").withRequiredArg().ofType(String.class).describedAs("number of generated queries");
		op.accepts("nr", "number of results per query").withRequiredArg().ofType(String.class).describedAs("number of results per query");
		
		OptionSet os = op.parse(args);
		
		if (!os.has("id")) {
			op.printHelpOn(System.out);
			return;
		}
		
		
		String indexDirectory = (String)os.valueOf("id");
		String resultDirectory = (String)os.valueOf("rd");
		String queriesDirectory = (String)os.valueOf("qd");
		int numberOfQueries = Integer.parseInt((String)os.valueOf("nq"));
		int numberOfResultsPerQuery = Integer.parseInt((String)os.valueOf("nr"));
		
		File queriesDirectoryFile = new File(queriesDirectory);
		File[] queryFiles = queriesDirectoryFile.listFiles(new FileFilter() {
			@Override
			public boolean accept(File file) {
				return file.getAbsolutePath().endsWith(".txt");
			}
		});
		
		 
		IndexReader ir = new IndexReader(new IndexDirectory(indexDirectory));
		TopKQueryEvaluator topKQueryEvaluator = new TopKQueryEvaluator(ir);
		
		
		for(File queryFile : queryFiles){
			String queryFileName = queryFile.getName();
			String query = getQuery(queryFile);
			
			log.debug("processing query file = " + queryFileName + " , " + " query = " + query);
			System.out.println("processing query file = " + queryFileName + " , " + " query = " + query);
			
			KeywordQuery keywordQuery = new KeywordQuery(queryFileName, query);
			
			Set<Row> rows =  topKQueryEvaluator.evaluate(keywordQuery, numberOfQueries , numberOfResultsPerQuery);
			
			File resultFile = new File(resultDirectory, queryFileName);
			resultFile.createNewFile();
			PrintWriter writer = new PrintWriter(resultFile);
			writer.println("# " + query);
			
			for(Row row : rows){
				writer.println(row.toString());
			}
			
			writer.flush();
			writer.close();
		}
		ir.close();
	}
	
	private static String getQuery(File queryFile) throws IOException, FileNotFoundException{
		BufferedReader reader = new BufferedReader(new FileReader(queryFile));
		String queryLine = reader.readLine();
		return queryLine.substring(queryLine.indexOf("#") + 1).trim();
	}
}
